package com.czk.springcloud.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 添加拦截器拦截请求
 *
 * @Author:ChenZhangKun
 * @Date: 2021/7/18 22:29
 */
@Configuration
public class InterceptorConfig {

    @Bean
    public RequestInterceptor requestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                // 拿到路由
                String url = template.url();
                // 替换环境变量
                if (url.contains("$env")) {
                    url = url.replace("$env", route(template));
                    System.out.println(url);
                    template.uri(url);
                }
                if (url.startsWith("//")) {
                    url = "http:" + url;
                    template.target(url);
                    template.uri("");
                }
            }

            /**
             * 这里是自己路由方法
             * @param template
             * @return
             */
            private CharSequence route(RequestTemplate template) {
                return "payment";
            }
        };
    }
}
